home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 020 / speechterm / speech.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  8KB  |  295 lines

  1. /* Here is a sample speech demo program that compiles on
  2.  * Amiga (Lattice) C.  It can be thought of as a stripped
  3.  * down version of the speechtoy (lucas).  I haven't 
  4.  * provided the graphics for the drawing of the mouth,
  5.  * but the access to the mouth variables is shown here.
  6.  * 
  7.  * It is the sample program from the rev 1.1 ROM KERNEL
  8.  * manual, now at the printers.
  9.  * 
  10.  * Rob Peck.   
  11.  *
  12.  * This code may be freely utilized to create programs for the Amiga.
  13.  */
  14.  
  15.  
  16.  
  17. #include "exec/types.h"
  18. #include "exec/exec.h"
  19.  
  20. #include "exec/nodes.h"
  21. #include "exec/lists.h"
  22. #include "exec/memory.h"
  23. #include "exec/interrupts.h"
  24. #include "exec/ports.h"
  25. #include "exec/libraries.h"
  26. #include "exec/io.h"
  27. #include "exec/tasks.h"
  28. #include "exec/execbase.h"
  29.  
  30. #include "devices/narrator.h"
  31. #include "libraries/translator.h"
  32.  
  33. struct MsgPort *readport=0;
  34. struct MsgPort *writeport=0;
  35.  
  36. extern struct MsgPort *CreatePort();
  37. extern struct IORequest *CreateExtIO();    
  38.  
  39. struct narrator_rb *writeNarrator=0;
  40. struct mouth_rb *readNarrator=0;
  41. struct Library *TranslatorBase=0;
  42. UBYTE outputstring[500];    /* place to put the translation */
  43. SHORT rtnCode;            /* return code from function */
  44. SHORT readError;
  45. SHORT writeError;
  46. SHORT error;
  47. BYTE  audChanMasks[4] = { 3,5,10,12 }; /* which channels to use */
  48.  
  49. #define CANT_OPEN_TRANSLATOR -100
  50. #define CANT_OPEN_NARRATOR -200
  51. #define CREATE_PORT_PROBLEMS -300
  52. #define CREATE_IO_PROBLEMS -400
  53. #define CANT_PERFORM_WRITE -500
  54. #define REVISION 1
  55.  
  56. extern struct Library *OpenLibrary();
  57.  
  58. say_string(Txt,done)
  59.   UBYTE *Txt;
  60.   int done;
  61. {
  62.   static FirstTime = 1;
  63.  
  64.   if (FirstTime) {
  65.     FirstTime = 0;
  66.     TranslatorBase = OpenLibrary("translator.library",REVISION);
  67.     if(TranslatorBase == NULL) exit (CANT_OPEN_TRANSLATOR);
  68.     rtnCode = Translate(Txt,strlen(Txt)+1,outputstring,500);
  69.     error = rtnCode + 100;
  70.     if(rtnCode != 0) goto cleanup0;
  71.     
  72.     writeport = CreatePort(0,0);
  73.     if(writeport == NULL) { error=CREATE_PORT_PROBLEMS; goto cleanup1; }
  74.     readport = CreatePort(0,0);
  75.     if(readport == NULL) { error=CREATE_PORT_PROBLEMS; goto cleanup2; }
  76.     writeNarrator = (struct narrator_rb *)CreateExtIO(writeport,
  77.                     sizeof(struct narrator_rb));
  78.     if(writeNarrator == NULL) { error=CREATE_IO_PROBLEMS; goto cleanup3; }
  79.     readNarrator = (struct mouth_rb *)CreateExtIO(readport,
  80.                     sizeof(struct mouth_rb));
  81.  
  82.     if(readNarrator == NULL) { error=CREATE_IO_PROBLEMS; goto cleanup4; }
  83. /* SET UP THE PARAMETERS FOR THE WRITE-MESSAGE TO THE NARRATOR DEVICE */
  84.  
  85.     /* show where to find the channel masks */
  86.     writeNarrator->ch_masks = (audChanMasks);
  87.  
  88.     /* and tell it how many of them there are */
  89.     writeNarrator->nm_masks = sizeof(audChanMasks);    
  90.  
  91.     /* tell it where to find the string to speak */    
  92.     writeNarrator->message.io_Data = (APTR)outputstring;
  93.  
  94.     /* tell it how many characters the translate function returned */
  95.     writeNarrator->message.io_Length = strlen(outputstring);
  96.  
  97.     /* if nonzero, asks that mouths be calculated during speech */
  98.     writeNarrator->mouths = 1;
  99.  
  100.     /* tell it this is a write-command */
  101.     writeNarrator->message.io_Command = CMD_WRITE;
  102.  
  103.     /* select rate of 165 words/min. */
  104.  
  105.     writeNarrator->rate = (UWORD)165;
  106.  
  107. /* Open the device  */
  108.  
  109.     error = OpenDevice("narrator.device", 0, writeNarrator, 0);
  110.     if(error != 0) goto cleanup4;
  111.  
  112. /* SET UP THE PARAMETERS FOR THE READ-MESSAGE TO THE NARRATOR DEVICE */
  113.  
  114.     /* tell narrator for whose speech a mouth is to be generated */
  115.     readNarrator->voice.message.io_Device = 
  116.         writeNarrator->message.io_Device;
  117.     readNarrator->voice.message.io_Unit = 
  118.         writeNarrator->message.io_Unit;
  119.  
  120.     readNarrator->width = 0;
  121.     readNarrator->height = 0;    /* initial mouth parameters */
  122.  
  123.     readNarrator->voice.message.io_Command = CMD_READ;
  124.         /* initial error value */
  125.     readNarrator->voice.message.io_Error = 0;    
  126.  
  127. /* Send an asynchronous write request to the device */
  128.  
  129.     writeError = SendIO(writeNarrator);
  130.     if(writeError != NULL) { error=CANT_PERFORM_WRITE; goto cleanup5; }
  131.     /* return immediately, run tasks concurrently */
  132.  
  133. /* keep sending reads until it comes back saying "no write in progress" */
  134.  
  135.     while((readError = readNarrator->voice.message.io_Error) != 
  136.         ND_NoWrite)
  137.     {
  138.         DoIO(readNarrator);
  139.         /* put task to sleep waiting for a different
  140.          * mouth shape or return of the message block
  141.          * with the error field showing no write in
  142.          * process
  143.          */
  144.     }
  145.  
  146.     Delay(30);
  147.      return;
  148.    }
  149.    if ( !done ) {
  150.     rtnCode = Translate(Txt,strlen(Txt)+1,outputstring,500);
  151. /*    writeNarrator->sex = FEMALE;
  152.     writeNarrator->pitch = MAXPITCH;  /* raise pitch from default value */
  153.     writeNarrator->message.io_Data = (APTR)outputstring;
  154.     writeNarrator->message.io_Length = strlen(outputstring);
  155.     DoIO(writeNarrator);
  156.     
  157.     Delay(30);
  158.     return;
  159.   }
  160.  
  161.     cleanup5:
  162.     if(writeNarrator != 0)
  163.         CloseDevice(writeNarrator);
  164.                 /* terminate access to the device */
  165.  
  166.     /* now return system memory to the memory allocator */ 
  167.  
  168.     cleanup4:
  169.     if(readNarrator != 0)
  170.         DeleteExtIO(readNarrator,sizeof(struct mouth_rb));
  171.     cleanup3:
  172.     if(writeNarrator != 0)
  173.         DeleteExtIO(writeNarrator,sizeof(struct narrator_rb));
  174.     cleanup2:
  175.     if(readport != 0)
  176.         DeletePort(readport);
  177.     cleanup1:
  178.     if(writeport != 0)
  179.         DeletePort(writeport);
  180.     cleanup0:
  181.     if(TranslatorBase != 0)
  182.            CloseLibrary(TranslatorBase);
  183.                 /* terminate access to the library */
  184.     
  185.     if(error != 0) exit(error);
  186. } /* end of test */    
  187.  
  188. /***********************************************************************
  189. *
  190. *    Exec Support Function -- Extended IO Request
  191. *
  192. ***********************************************************************/
  193.  
  194. extern APTR AllocMem();
  195.  
  196. /****** exec_support/CreateExtIO **************************************
  197. *
  198. *   NAME    
  199. *    CreateExtIO() -- create an Extended IO request
  200. *
  201. *   SYNOPSIS
  202. *    ioReq = CreateExtIO(ioReplyPort,size);   
  203. *
  204. *   FUNCTION
  205. *    Allocates memory for and initializes a new IO request block
  206. *    of a user-specified number of bytes.
  207. *
  208. *   INPUTS
  209. *    ioReplyPort - a pointer to an already initialized
  210. *        message port to be used for this IO request's reply port.
  211. *
  212. *   RESULT
  213. *    Returns a pointer to the new block.  Pointer is of the type
  214. *    struct IORequest.
  215. *
  216. *    0 indicates inability to allocate enough memory for the request block
  217. *    or not enough signals available.
  218. *
  219. *   EXAMPLE
  220. *    struct IORequest *myBlock;
  221. *    if( (myBlock = CreateExtIO(myPort,sizeof(struct IOExtTD)) == NULL)
  222. *        exit(NO_MEM_OR_SIGNALS);
  223. *
  224. *    example used to allocate space for IOExtTD (trackdisk driver
  225. *    IO Request block for extended IO operations).
  226. *
  227. *   SEE ALSO
  228. *    DeleteExtIO
  229. *
  230. ***********************************************************************/
  231.  
  232. struct IORequest *CreateExtIO(ioReplyPort,size)
  233.     struct MsgPort *ioReplyPort;
  234.     LONG size;
  235. {
  236.     struct IORequest *ioReq;
  237.  
  238.     if (ioReplyPort == 0)
  239.     return ((struct IORequest   *) 0);
  240.  
  241.     ioReq = (struct IORequest *)AllocMem (size, MEMF_CLEAR | MEMF_PUBLIC);
  242.  
  243.     if (ioReq == 0)
  244.     return ((struct IORequest   *) 0);
  245.  
  246.     ioReq -> io_Message.mn_Node.ln_Type = NT_MESSAGE;
  247.     ioReq -> io_Message.mn_Node.ln_Pri = 0;
  248.  
  249.     ioReq -> io_Message.mn_ReplyPort = ioReplyPort;
  250.     ioReq -> io_Message.mn_Length = (size - sizeof(struct Message));
  251.                         /* new (rap) */
  252.     return (ioReq);
  253. }
  254.  
  255. /****** exec_support/DeleteExtIO **************************************
  256. *
  257. *   NAME
  258. *    DeleteExtIO() - return memory allocated for extended IO request
  259. *
  260. *   SYNOPSIS
  261. *    DeleteExtIO(ioReq,size);
  262. *
  263. *   FUNCTION
  264. *    See summary line at NAME.  Also frees the signal bit which
  265. *    had been allocated by the call to CreateExtIO.
  266. *
  267. *   INPUTS
  268. *    A pointer to the IORequest block whose resources are to be freed.
  269. *
  270. *   RESULT
  271. *    Frees the memory.  Returns (no error conditions shown)
  272. *
  273. *   EXAMPLE
  274. *    struct IORequest *myBlock;
  275. *    DeleteExtIO(myBlock,(sizeof(struct IOExtTD)));
  276. *        
  277. *    example shows that CreateExtIO had been used to create a trackdisk
  278. *    (extended) IO Request block.
  279. *
  280. *   SEE ALSO
  281. *    CreateExtIO
  282. *
  283. **************************************************************************/
  284.  
  285. DeleteExtIO(ioExt,size)
  286.     struct IORequest *ioExt;
  287.     LONG size;
  288. {
  289.     ioExt -> io_Message.mn_Node.ln_Type = 0xff;
  290.     ioExt -> io_Device = (struct Device *) -1;
  291.     ioExt -> io_Unit = (struct Unit *) -1;
  292.  
  293.     FreeMem (ioExt, size);
  294. }
  295.